#include "LoginServer.h"
#include "../log.h"
#include "../ConfigMgr.h"
#include "../DB/Redis/RedisConPool.h"
#include "../DB/MySql/MySqlConPool.h"
#include "../GrpcClient/StatusGrpcClient.h"
#include "../Singleton.h"
#include <cstdint>

uint16_t GetGateServerPort(std::shared_ptr<Config> const& config, const std::string& serverName)
{
    if(serverName == "GateServer_01")
    {
        return config->GateServerInfo_01.Port;
    }
    else if(serverName == "GateServer_02")
    {
        return config->GateServerInfo_02.Port;
    }
    
    return 0;
}

int main(void)
{
	//设置日志级别
	set_log_level(log_level::trace);
	//设置日志目录
	// set_log_file("./log.log");

    auto config = ConfigMgr::GetConfig();

    //初始化Redis连接池，并显示
    Singleton<RedisConPool>::GetInstance()->show();

    //初始化MySQL连接池，并显示
    Singleton<MySqlPool>::GetInstance()->show();

    //初始化Status gRPC 客户端
    Singleton<StatusGrpcClient>::GetInstance();

    uint16_t port = config->LoginServerInfo.Port;

    try
    {
        boost::asio::io_context ioc;
        boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
        signals.async_wait([&](boost::system::error_code error, int si)
            {
                if(error)
                {
                    log_error("main signal error: {}", error.message());
                    return;
                }
                
                ioc.stop();
            });
        
        std::make_shared<LoginServer>(ioc, port)->Start();
        ioc.run();
    }
    catch (const std::exception& e)
    {
        log_error("main exception: {}", e.what());
        return -1;
    }

}
